package cn.itcast.listener;

import cn.itcast.utils.MailUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 邮件发送监听器
 */
@Component
public class EmailListener implements MessageListener{
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onMessage(Message message) {
        //1.接收消息
        byte[] body = message.getBody();

        //2.接收并转换
        try {
            Map map = objectMapper.readValue(body,Map.class);

            //3.获取数据
            String username = (String)map.get("username");
            String email = (String)map.get("email");
            String companyName = (String)map.get("companyName");

            //4.发送邮件
            String title = "Saas-Export入职欢迎邮件";
            String content = "亲爱的"+username+"，欢迎加入"+companyName+"的大家庭，让我们一起携手共创未来！";

            MailUtil.sendMsg(email,title,content);

            System.out.println("邮件发送完毕!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
